package old;

import java.io.IOException;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Transaction;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;


public class SupprimerGroupeServlet extends HttpServlet {

	private static final long serialVersionUID = -2502133846514921463L;
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)	throws IOException {
		resp.setContentType("text/plain");
		String nomGroupeASupprimer = req.getParameter("groupeASupprimer");
		DatastoreService datastore= DatastoreServiceFactory.getDatastoreService();
		Transaction tx = datastore.beginTransaction();
		try {
			Key key = KeyFactory.createKey("Groupes",nomGroupeASupprimer);
			datastore.delete(key);
			tx.commit();
		} finally {
		    if (tx.isActive()) {
		        tx.rollback();
		    }
		}
		
		try {
			
			Query q = new Query("Membres");	
			PreparedQuery pq = datastore.prepare(q);
			for(Entity result : pq.asIterable()){
				tx = datastore.beginTransaction();
				result.removeProperty(nomGroupeASupprimer);
				datastore.put(result);
				tx.commit();
			}
			
		} finally {
		    if (tx.isActive()) {
		        tx.rollback();
		    }
		}
		resp.sendRedirect("/panneauAdministration/groupes.jsp");
	}
}
